﻿//*********************************************************************
//
//  文件名：RedisHelp
//
//  作者： LW
//
//  作用：Redis缓存使用
//
//  日期：2015/9/15 15:55:59
//
//*********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Framework.Cache.Redis;
using System.Configuration;
using YouTravel.ViewModels;
using YouTravel.Framework.Enums;
using Framework.Common.Exceptions;

namespace Framework.Common
{
    /// <summary>
    /// RedisHelp
    /// </summary>
    public static class RedisHelp
    {
        private static string[] readWriteHosts = ConfigurationManager.AppSettings["readWriteHosts"].Split(';');
        private static string[] readOnlyHosts = ConfigurationManager.AppSettings["readOnlyHosts"].Split(';');
        private static RedisBase redisBase = new RedisBase(readWriteHosts, readOnlyHosts);

        /// <summary>
        /// 缓存登录用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static bool CacheLoginUser(UserViewModel user)
        {
            string key = CacheType.Login + user.ID.ToString();
            try
            {
                //移除缓存
                bool flag = redisBase.Item_Remove(key);
            }
            catch (Exception)
            {

            }
            return redisBase.Item_Set<UserViewModel>(key, user);
        }

        /// <summary>
        /// 登录用户 刷新缓存
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static bool RefreshLoginUserCache(UserViewModel user)
        {
            return CacheLoginUser(user);
        }

        /// <summary>
        /// 获取登录用户
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public static UserViewModel GetLoginUserCache(int userID)
        {
            string key = CacheType.Login + userID;
            var user = redisBase.Item_Get<UserViewModel>(key);
            //if (user == null)
            //throw new AuthorizeException("用户未登录或登录状态失效,请重新登录");
            return user;
        }
        /// <summary>
        /// 获取登录用户
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public static UserViewModel GetLoginUserCacheNotNull(int userID)
        {
            string key = CacheType.Login + userID;
            var user = redisBase.Item_Get<UserViewModel>(key);
            if (user == null)
                throw new AuthorizeException("用户未登录或登录状态失效,请重新登录");
            return user;
        }
        const string key = "IPList";
        public static void SetIPList<T>(string ip, T value)
        {
            redisBase.Hash_Set(key, ip, value);
        }

        public static T GetIPListValue<T>(string ip)
        {
            return redisBase.Hash_Get<T>(key, ip);
        }
        public static void DelIPList(string ip)
        {
            redisBase.Hash_Remove(key, ip);
        }
        const string keyIpBlack = "IpBlackList";
        public static void SetIpBlackList<T>(string ip, T value)
        {
            redisBase.Hash_Set(keyIpBlack, ip, value);
        }

        public static T GetIpBlackListValue<T>(string ip)
        {
            return redisBase.Hash_Get<T>(keyIpBlack, ip);
        }
        public static void DelIpBlackList(string ip)
        {
            redisBase.Hash_Remove(keyIpBlack, ip);
        }
    }
}
